21.6 Establishing the Softirq Framework
Within the Linux kernel, software interrupts (Softirqs) serve as the core foundational mechanism for bottom-half interrupt processing. They are predominantly utilized to orchestrate and handle deferred operational tasks that enforce strict real-time execution constraints but do not exhibit the extreme microarchitectural urgency characteristic of top-half hard interrupts. The softirq subsystem empowers the kernel to shunt non-critical execution logic out of the hot path and process it later with local CPU interrupts fully re-enabled, drastically driving up global system throughput. Consequently, softirqs function as a highly optimized bottom-half interrupt mechanism engineered to perfectly balance low-latency interrupt response against computational task-handling overhead.
When a peripheral hardware interrupt manifests (such as a network interface card registering inbound packet arrivals), the CPU core instantly enters a hard interrupt context (the top half). Because a hard interrupt context forces the processor core to completely disable its local interrupt response, allowing a driver to execute long-running lookup or processing logic within this phase would inevitably trigger severe system packet loss or pipeline thrashing. To eliminate this vulnerability, the top half executes exclusively the most immediate, critical tasks—such as latching physical device registers, clearing hardware status flags, and raising a softirq vector. Heavy, non-atomic processing logic (such as network protocol stack parsing or timer callback execution) is safely deferred to run asynchronously within a softirq context.
The available categories of softirqs are statically mapped out during compile time via an explicit C-language enum definition and do not support dynamic runtime registration. The kernel currently exposes 10 distinct types of softirqs. A unique architectural benefit of softirqs is that an identical category of software interrupt can execute concurrently across multiple CPU cores simultaneously. This design choice implies that softirq handler functions must be fully re-entrant and must incorporate robust locking primitives to safeguard shared resources. Once a CPU core commences processing a softirq vector, its execution path cannot be preempted by an alternative softirq, though it remains fully eligible to be interrupted by inbound hard exceptions.
Softirqs are most commonly invoked via invoke_softirq() at the precise moment a hard interrupt context finishes execution, making this the primary execution funnel. If a softirq processing loop runs continuously over an extended window, the kernel automatically awakens a per-CPU background daemon thread named ksoftirqd to offload the remaining execution queue asynchronously, preventing standard process scheduling starvation. Furthermore, softirq processing loops can be triggered when a thread explicitly calls local_bh_enable(), which frequently occurs inside peripheral network driver stacks.
The primary softirq vectors mapped within the Linux kernel encompass:
| Vector | Constant Name | Functional Description |
|---|---|---|
| 0 | HI_SOFTIRQ | High-priority Tasklets |
| 1 | TIMER_SOFTIRQ | Timer Wheel and high-resolution timer processing |
| 2 | NET_TX_SOFTIRQ | Outbound network packet transmission processing |
| 3 | NET_RX_SOFTIRQ | Inbound network packet reception (typically the highest-stress vector) |
| 4 | BLOCK_SOFTIRQ | Block device (disk filesystem) I/O processing |
| 7 | SCHED_SOFTIRQ | Core scheduler multi-core load balancing |
| 9 | RCU_SOFTIRQ | RCU garbage collection and callback draining |
A subsystem flags a specific softirq vector as active by calling raise_softirq(), which updates an internal bitmask to transition that slot into a "pending" state. When the interrupt handling path enters irq_exit(), local hardware interrupts remain strictly disabled. Upon discovering that a pending softirq bit is active, irq_exit() dispatches invoke_softirq(), which ultimately resolves to invoke the core execution loop __do_softirq().
The function __do_softirq() immediately re-enables local hard interrupts on the CPU core, ensuring that the processor can smoothly catch and respond to newly arriving hardware exception signals. Following this re-enabling pass, __do_softirq() fires the concrete softirq handler routines corresponding to the active pending bits. Once the softirq processing loops conclude, the engine disables local hard interrupts once again and returns execution control back to fulfill the residual tracking steps of irq_exit(), including flushing internal status flags. After irq_exit() finishes its operations, the kernel processes basic cleanup metrics and triggers the final hardware interrupt return instruction (iret or its architecture equivalent), successfully concluding the hard interrupt processing pass.
Because identical softirq vectors can execute concurrently across multiple CPU cores, their handler callbacks must be fully re-entrant and must leverage synchronization primitives (such as spinlocks) to safeguard shared memory structures. Softirqs operate strictly within an atomic execution context; dropping into a blocking sleep phase, stall phase, or invoking any helper functions prone to triggering a scheduling pass is strictly prohibited. The runtime priority profile of a softirq sits strictly higher than standard process threads but remains subordinate to hard exception vectors.
Within the Linux kernel, softirq_init() serves as the definitive initialization handler for the softirq subsystem. This function is declared within the source file git/kernel/softirq.c, and its definition is implemented as follows:
void __init softirq_init(void)
{
int cpu;
for_each_possible_cpu(cpu) {
per_cpu(tasklet_vec, cpu).tail = &per_cpu(tasklet_vec, cpu).head;
per_cpu(tasklet_hi_vec, cpu).tail = &per_cpu(tasklet_hi_vec, cpu).head;
}
open_softirq(TASKLET_SOFTIRQ, tasklet_action);
open_softirq(HI_SOFTIRQ, tasklet_hi_action);
}
The core objective of softirq_init() is to bootstrap the per-CPU lookaside data structures consumed to manage softirqs and tasklets, while registering the baseline software interrupt handlers. It iterates across all possible CPU cores configured in the system, forcing the local links of both tasklet_vec (standard-priority tasklets) and tasklet_hi_vec (high-priority tasklets) to initialize as vacant chains by aligning their tail pointers directly to their head descriptors. Following this step, it invokes open_softirq() to bind the handler function tasklet_action to the TASKLET_SOFTIRQ vector, and maps the handler function tasklet_hi_action to drive the HI_SOFTIRQ vector.
Because softirqs support symmetric parallel execution across multiple processor cores simultaneously, strict locking disciplines must be engineered into any custom softirq processing handler.
Softirqs are typically evaluated upon a hard interrupt return sequence, inside the dedicated background kernel threads ksoftirqd, or at specific execution checkpoints that explicitly query the status of unhandled softirq bitmasks. They run entirely within an atomic context, meaning that dropping into a low-power sleep phase or invoking a blocking routine will instantly crash the system context. Figure 21-2 details the comprehensive workflow of an interrupt processing loop handling softirqs, tracing the execution path as it streams across generic interrupt processing, hard top-half handling, and bottom-half softirq execution.
Figure 21-2 Procedural Processing Path of a Software Interrupt